package edu.columbia.aicheckers;

import edu.columbia.aicheckers.agent.*;
import edu.columbia.aicheckers.agent.dummy.*;
import edu.columbia.aicheckers.model.Color;

/**
 * Main method for running the program with command line arguments. 
 * Please see readme for details
 * @author Marshall
 *
 */
public class DriverCL {
	
	private static int numMatches = 100;

	public static void main(String[] args) {
		
		int numWhiteWins = 0;
		int numBlackWins = 0;

		String usage = "Usage: java DriverCL agent1 agent2 numMatches\n" +
		"Use the following to specify agents:\n" +
		"D for DummyAgent\n" +
		"R for RuleBasedAgent\n" +
		"M for Minimax\n" +
		"I for IntelliAgent\n" +
		"G for GeneticAgent\n" +
		"C for ConsoleAgent\n";
		
		if (args.length < 2 || args.length > 3) {
			System.err.println(usage);
			System.exit(0);
		}
		
		IAgent Agent1 = null;
		IAgent Agent2 = null;
		
		if (args[0].equals("D")) Agent1 = new DummyAgent();
		else if (args[0].equals("R")) Agent1 = new RuleBasedAgent();
		else if (args[0].equals("I")) Agent1 = new IntelliAgent();
		else if (args[0].equals("M")) Agent1 = new MinimaxAgent(5);
		else if (args[0].equals("G")) Agent1 = new GeneticAgent();
		else if (args[0].equals("C")) Agent1 = new ConsoleAgent();
		else {System.err.println(usage); System.exit(0);}
		
		if (args[1].equals("D")) Agent2 = new DummyAgent();
		else if (args[1].equals("R")) Agent2 = new RuleBasedAgent();
		else if (args[1].equals("I")) Agent2 = new IntelliAgent();
		else if (args[1].equals("M")) Agent2 = new MinimaxAgent(5);
		else if (args[1].equals("G")) Agent2 = new GeneticAgent();
		else if (args[1].equals("C")) Agent2 = new ConsoleAgent();
		else {System.err.println(usage); System.exit(0);}
		
		
		if (args.length == 3) {
			try {
				numMatches = Integer.parseInt(args[2]);
			}
			catch (Exception E) {System.err.println("Please enter a numeric value for the number of matches.");}
		}
			
		for(int i = 0;i < numMatches; i++)
		{
			CheckerGame game = new CheckerGame(Agent1, Agent2);
			game.setPrintDebugInfo(true);
			Color winner = game.start();
			if(winner == Color.White)
				numWhiteWins++;
			else if(winner == Color.Black)
				numBlackWins++;
			System.out.println(i + " Game Ended");
			System.out.println("White wins: " + numWhiteWins);
			System.out.println("Black wins: " + numBlackWins);
		}
		
		System.out.println("% White wins: " + 100.0*((double)numWhiteWins/(double)numMatches));
		System.out.println("% Black wins: " + 100.0*((double)numBlackWins/(double)numMatches));

		
	}

}
